#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/5/13 10:55
# @USER    : 717-3
# @File    : ReverseInteger.py
# @Software: PyCharm
# @Version  : Python-3.6
# @TASK:


class Solution:
    def reverse(self, x: int) -> int:
        """
        Given a 32-bit signed integer, reverse digits of an integer.

        :param x: a 32-bit signed integer
        :return: reverse digits of an integer

        Example 1:
            Input: 123

            Output: 321

        Example 2:
            Input: -123

            Output: -321

        Example 3:
            Input: 120

            Output: 21

        Note:
            Assume we are dealing with an environment which could only store integers within
            the 32-bit signed integer range: [−231,  231 − 1].
            For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
        """
        xstr = str(x)
        xre = xstr[::-1]
        if x < 0:
            xre = '-' + xre[:-1]
        xout = int(xre)
        if xout < -2**31 or xout > 2**31 - 1:
            return 0
        else:
            return xout


if __name__ == '__main__':
    S = Solution()
    # Q7
    sample = 123
    print(S.reverse(sample))
    print('done')
